package service

import (
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
	"mojieshi/define"
	"mojieshi/models"
	"net/http"
	"time"
)

// 获取历史搜索
func GetSearch(c *gin.Context) {
	var jsonData Newdata
	err := c.ShouldBindJSON(&jsonData)
	if err != nil {
		define.Response.ResultFail(c, 10000, define.JsonDataX, "")
		return
	}
	var list []models.MSearch
	err = models.DB.Model(new(models.MSearch)).Where("mid=? AND aid=?", jsonData.Mid, jsonData.Aid).Limit(jsonData.Limit).Order("id desc").Find(&list).Error
	if err != nil {
		define.Response.ResultFail(c, 10001, define.JsonDataX, "")
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"success": true,
		"code":    0,
		"data":    list,
	})
}

//新增历史搜索

func AddSearch(c *gin.Context) {
	var jsonData models.MSearch
	err := c.ShouldBindJSON(&jsonData)
	if err != nil {
		define.Response.ResultFail(c, 10000, define.JsonDataX, "")
		return
	}
	var list []models.MSearch
	var count int64
	err = models.DB.Model(new(models.MSearch)).Where("mid=? AND name=?", jsonData.Mid, jsonData.Name).Count(&count).Find(&list).Error
	if err != nil {
		define.Response.ResultFail(c, 10001, define.JsonDataX, "")
		return
	}
	if count > 0 {
		c.JSON(http.StatusOK, gin.H{
			"success": true,
			"code":    0,
			"msg":     "成功！",
		})
		return
	}
	var data models.MSearch
	data.Name = jsonData.Name
	data.Aid = jsonData.Aid
	data.Mid = jsonData.Mid
	data.CreatedAt = int(time.Now().Unix())
	err = models.DB.Model(new(models.MSearch)).Create(&data).Error
	if err != nil {
		define.Response.ResultFail(c, 10001, define.JsonDataX, "")
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"success": true,
		"code":    0,
		"msg":     "成功！",
	})
}

// 获取热门搜索
func GetHotSearch(c *gin.Context) {
	var jsonData Newdata
	err := c.ShouldBindJSON(&jsonData)
	if err != nil {
		define.Response.ResultFail(c, 10000, define.JsonDataX, "")
		return
	}
	var list []models.MHOTSearch
	err = models.DB.Model(new(models.MHOTSearch)).Where("aid=?", jsonData.Aid).Limit(jsonData.Limit).Order("sum desc,id").Find(&list).Error
	if err != nil {
		define.Response.ResultFail(c, 10001, define.JsonDataX, "")
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"success": true,
		"code":    0,
		"data":    list,
	})
}

// 热门搜索+1
func AddHotSearch(c *gin.Context) {
	var jsonData models.MHOTSearch
	err := c.ShouldBindJSON(&jsonData)
	if err != nil {
		define.Response.ResultFail(c, 10000, define.JsonDataX, "")
		return
	}
	err = models.DB.Model(new(models.MHOTSearch)).Where("id=?", jsonData.ID).Update("sum", gorm.Expr("sum +?", 1)).Error
	if err != nil {
		define.Response.ResultFail(c, 10001, define.JsonDataX, "")
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"success": true,
		"code":    0,
		"msg":     "成功！",
	})
}

// 搜索结果
func GetSearchShow(c *gin.Context) {
	var jsonData Newdata
	err := c.ShouldBindJSON(&jsonData)
	if err != nil {
		define.Response.ResultFail(c, 10000, define.JsonDataX, "")
		return
	}
	var list []models.MFuProduct
	var count int64
	if jsonData.Page == 0 {
		jsonData.Page = -1
	}
	if jsonData.Limit == 0 {
		jsonData.Limit = -1
	}
	offsetVal := (jsonData.Page - 1) * jsonData.Limit
	if jsonData.Page == -1 && jsonData.Limit == -1 {
		offsetVal = -1
	}
	err = models.DB.Model(new(models.MFuProduct)).Where("name LIKE ? or sub_name  LIKE ?", "%"+jsonData.Name+"%", "%"+jsonData.Name+"%").Count(&count).Limit(jsonData.Limit).Offset(offsetVal).Order("sort desc,id").Find(&list).Error
	if err != nil {
		define.Response.ResultFail(c, 10001, define.JsonDataX, "")
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"success": true,
		"code":    0,
		"data": map[string]interface{}{
			"list":  list,
			"count": count,
		},
	})
}
